﻿using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ZhCun.WebCommon.AL;
using ZhCun.WebCommon.WebApi;

namespace ZhCun.WebCommon
{
    public class SwaggerOperationFilter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
            {
                operation.Parameters = new List<IParameter>();
            }
            if (context.ApiDescription.TryGetMethodInfo(out MethodInfo methodInfo))
            {
                if (methodInfo.ReflectedType.GetCustomAttributes<LoginAuthorizeAttribute>().Any() &&
                    !methodInfo.CustomAttributes.Any(t => t.AttributeType == typeof(NonLoginVerifyAttribute)))
                {
                    operation.Parameters.Add(new NonBodyParameter
                    {
                        Name = Constants.TOKEN_NAME,
                        In = "header",
                        Type = "string",
                        Required = true,
                        Description = "请输入Token"
                    });
                }
                operation.Parameters.Add(new NonBodyParameter
                {
                    Name = Constants.DATA_ENCRYPT_KEY_VALUE,
                    In = "header",
                    Type = "string",
                    Required = AppCommonConfig.AppSettings.DataEncrypt,
                    Description = "rsa加密的随机aes密钥"
                });
            }
        }
    }
}